package com.example.mvvm.pokemon.ui.list

import android.os.Bundle
import android.view.View
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.atom.module.mvvm.base.AbstractBaseActivity
import com.atom.module.mvvm.extren.isNo
import com.atom.module.mvvm.extren.isYes
import com.atom.module.mvvm.util.AbstractAdapter
import com.atom.module.mvvm.util.AbstractViewHolder
import com.atom.module.mvvm.util.AdapterListener
import com.atom.module.utils.Logs
import com.bumptech.glide.Glide
import com.example.mvvm.BR
import com.example.mvvm.R
import com.example.mvvm.databinding.ActivityListBinding
import com.example.mvvm.pokemon.net.NewsCardData
import com.example.mvvm.pokemon.ui.ViewModelFactory
import com.scwang.smart.refresh.footer.ClassicsFooter
import com.scwang.smart.refresh.header.ClassicsHeader


class ListActivity : AbstractBaseActivity<ActivityListBinding, ListViewModel>() {

    override fun initContentView(savedInstanceState: Bundle?): Int {
        return R.layout.activity_list
    }

    override fun initVariableId(): Int {
        return BR._all
    }

    override fun initViewModel(): ListViewModel {
        return ViewModelProvider(
            this.viewModelStore,
            ViewModelFactory
        ).get(ListViewModel::class.java)
    }

    override fun initViewBinding(binding: ActivityListBinding, viewModel: ListViewModel) {
        super.initViewBinding(binding, viewModel)
        binding.also {
            // 布局刷新
            it.refreshLayout.setRefreshHeader(ClassicsHeader(this))
            it.refreshLayout.setRefreshFooter(ClassicsFooter(this))

            it.refreshLayout.setOnRefreshListener { refreshLayout ->
                refreshLayout.finishRefresh(1000 /*,false*/) //传入false表示刷新失败

            }
            it.refreshLayout.setOnLoadMoreListener { refreshlayout ->
                refreshlayout.finishLoadMore(2000 /*,false*/) //传入false表示加载失败
                val currSize = it.listView.adapter?.itemCount ?: 0
                val currPage = currSize.div(30) + 1
                viewModel.loadMoreNewsData(currPage)
            }
            it.listView.layoutManager = LinearLayoutManager(this)
            it.listView.adapter = object : AbstractAdapter<NewsCardData>(){
                override fun getItemViewType(position: Int, data: NewsCardData): Int {
                    return (position % 2 == 0).isYes {
                        R.layout.recycler_item_news_big_image
                    }.isNo {
                        R.layout.recycler_item_news
                    }
                }

                override fun onCreateViewHolder(
                    contentView: View,
                    viewType: Int
                ): AbstractViewHolder<NewsCardData> {
                    if (viewType == R.layout.recycler_item_news_big_image) {
                        return NewsBigViewHolder(contentView)
                    }
                    return NewsViewHolder(contentView)
                }
            }.also {
                it.setAdapterListener(object : AdapterListener<NewsCardData> {
                    override fun onItemClick(
                        view: View,
                        adapter: AbstractAdapter<NewsCardData>,
                        viewHolder: AbstractViewHolder<NewsCardData>,
                        data: NewsCardData
                    ) {
                        super.onItemClick(view, adapter, viewHolder, data)
                        Logs.e("点击了", viewHolder.mData?.date)
                    }

                    override fun onItemLongClick(
                        view: View,
                        adapter: AbstractAdapter<NewsCardData>,
                        viewHolder: AbstractViewHolder<NewsCardData>,
                        data: NewsCardData
                    ): Boolean {
                        Logs.e("长按了", viewHolder.mData?.date)
                        return super.onItemLongClick(view, adapter, viewHolder, data)
                    }
                })

            }
        }
    }

    class NewsViewHolder(view: View) :
        AbstractViewHolder<NewsCardData>(view) {

        override fun onBind(data: NewsCardData) {
            Glide.with(itemView)
                .load(data.thumbnail_pic_s)
                .placeholder(R.drawable.ic_launcher_background)
                .into(itemView.findViewById(R.id.avator))
        }
    }

    class NewsBigViewHolder(view: View) :
        AbstractViewHolder<NewsCardData>(view) {

        override fun onBind(data: NewsCardData) {
            Glide.with(itemView)
                .load(data.thumbnail_pic_s)
                .placeholder(R.drawable.ic_launcher_background)
                .into(itemView.findViewById(R.id.avator))
        }
    }

    override fun initData(view: View) {
        super.initData(view)
        viewModel.also {
            it.listdata.observe(this) { card ->
                val adapter = binding.listView.adapter
                if (adapter is AbstractAdapter<*>) {
                    (adapter as AbstractAdapter<NewsCardData>).addAll(
                        card.value.data!!
                    )
                }
            }
        }
    }
}